package com.origin.chaos.chat.controller;

import com.origin.chaos.chat.bean.vo.ChaosMessages;
import com.origin.chaos.chat.bean.vo.SendMessageVo;
import com.origin.chaos.chat.core.service.ChatService;
import com.origin.chaos.common.bean.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: tao.ding
 * @date 2021-08-17
 */
@RestController
@RequestMapping("/api/chat")
@Api(tags = "消息接口模块")
@Slf4j
@Validated
public class MessageController {

    private ChatService chatService;

    @PostMapping("/message")
    @ApiOperation("发送消息")
    public Result sendMessage(@Validated @RequestBody SendMessageVo vo) {
        this.chatService.sendMessage(vo.getMessage());
        return Result.defaultSuccess();
    }

    @GetMapping("/messages")
    @ApiOperation("反向查询历史消息")
    public Result<ChaosMessages> listMessages(@ApiParam(value = "是否第一次查询", required = true) @RequestParam("firstPage") boolean firstPage,
                                              @ApiParam(value = "开始索引 第一次查询的时候传0", required = true) @RequestParam("index") long index,
                                              @ApiParam(value = "查询消息条数", required = true) @RequestParam("size") long size) {
        return Result.ofBody(this.chatService.reverseSearchMessageList(firstPage, index, size));
    }

    @Autowired
    public void setChatService(ChatService chatService) {
        this.chatService = chatService;
    }
}
